#include <future>
#include <thread>
#include <chrono>
#include <iostream>
int Add(int x, int y) {
    std::cout << "add\n";
    return x + y;
}
int main()
{
    // std::future<int> ret = std::async(Add,11,22);
    // std::cout << "----------1-----------\n";
    // std::cout << "----------2-----------\n";
    // std::cout << ret.get() << "\n";

    // std::launch::deferred 只有当 ret.get 被执行时，不创建线程
    // std::launch::async 直接创建线程执行
    // 两者都需要保证在 ret.get 处完成任务，否则则阻塞等待
    // std::future<int> ret = std::async(std::launch::async,Add,11,22);
    std::future<int> ret = std::async(std::launch::async,Add,11,22);
    std::cout << "----------1-----------\n";
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << ret.get() << "\n";
    std::cout << "----------2-----------\n";
    
    return 0;
}